package com.wyc.edu.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wyc.commonutils.R;
import com.wyc.edu.entity.EduTeacher;
import com.wyc.edu.entity.vo.TeacherQuery;
import com.wyc.edu.service.TeacherService;

import com.wyc.edu.util.HandleUpload;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author WYC
 * @since 2020-05-26
 */
@Api(description="讲师管理")
@RestController
@RequestMapping("/edu/teacher")
public class TeacherController {
    @Autowired
    private TeacherService teacherService;
    @Autowired
    HandleUpload handleUpload;
    @ApiOperation(value = "所有讲师列表")
    @GetMapping("findAll")
    public R list(){
        List<EduTeacher> list = teacherService.list(null);
        return R.ok().data("items", list);
    }

    @ApiOperation(value = "根据ID删除讲师")
    @DeleteMapping("{id}")
    public R removeById(
            @ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable String id){
        boolean b = teacherService.removeById(id);
        if(b){
            return R.ok();
        }else {
            return R.error();
        }

    }

    @ApiOperation(value = "分页讲师列表")
    @PostMapping("{page}/{limit}")
    public R pageQuery(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,

            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,

            @ApiParam(name = "teacherQuery", value = "查询对象", required = false)
                    @RequestBody TeacherQuery teacherQuery){

        Page<EduTeacher> pageParam = new Page<>(page, limit);

        teacherService.pageQuery(pageParam, teacherQuery);
        List<EduTeacher> records = pageParam.getRecords();
        long total = pageParam.getTotal();

        return  R.ok().data("total", total).data("rows", records);
    }
    @ApiOperation(value = "新增讲师")
    @PostMapping(("addTeacher"))
    public R save(
            @ApiParam(name = "teacher", value = "讲师对象", required = true)
            @RequestBody EduTeacher teacher){

        teacherService.save(teacher);
        return R.ok();
    }
    @ApiOperation(value = "根据ID查询讲师")
    @GetMapping("getTeacher/{id}")
    public R getById(
            @ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable String id){
        EduTeacher teacher = teacherService.getById(id);
        return R.ok().data("teacher", teacher);
    }

    @ApiOperation(value = "根据ID修改讲师")
    //讲师修改功能
    @PostMapping("updateTeacher")
    public R updateTeacher( @ApiParam(name = "teacher", value = "讲师对象", required = true)
                                @RequestBody EduTeacher teacher) {
        if(!StringUtils.isEmpty(teacher.getAvatar())){
            EduTeacher byId = teacherService.getById(teacher.getId());
            if(!byId.getAvatar().equals(teacher.getAvatar())){
                System.out.println(byId.getAvatar()+"需要删除");
                handleUpload.deleteFile(byId.getAvatar());
            }
        }


        boolean flag = teacherService.updateById(teacher);
        if(flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }
}

